package com.weme.common.view

import android.graphics.Path
import android.graphics.RectF

class SmootRoundedPath {
    private val mRoundPath = Path()
    var proportion =100f
    private val ratioOffset: Float = 4.0f
    fun coerceAtMost(value: Float, maximumValue: Float): Float {
        return if (value > maximumValue) {
            maximumValue
        } else value
    }

    fun coerceAtLeast(value: Float, minimumValue: Float): Float {
        return if (value < minimumValue) {
            minimumValue
        } else value
    }


    fun getSmoothRoundPath(rectF: RectF, leftRadius: Float,topRadius: Float,rightRadius: Float,bottomRadius: Float): Path {

        val left = rectF.left
        val top = rectF.top
        val width = rectF.width()
        val height = rectF.height()



        // 开始画圆角的位置对比圆角大小的偏移比例
        val radiusOffsetRatio = 500f / proportion // 靠近圆弧两个端点的点的xy坐标比例
        val endPointRatio = 83f / proportion // 左上角第一条曲线第二个点x坐标的比例(其他三个点通过矩阵转换可以使用同样的比例）
        val firstCSecondPXRatio = 67f / proportion // 左上角第一条曲线第二个点Y坐标的比例（其他三个点通过矩阵转换可以使用同样的比例)
        val firstCSecondPYRatio = 4f / proportion // 左上角第一条曲线第三个点x坐标的比例（其他三个点通过矩阵转换可以使用同样的比例)
        val firstCThirdPXRatio = 51f / proportion // 左上角第一条曲线第三个点Y坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        val firstCThirdPYRatio = 13f / proportion // 左上角第二条曲线第一个点X坐标的比例（其他三个点通过矩阵转换可以使用同样的比例)
        val secondCFirstPXRatio = 34f / proportion // 左上角第二条曲线第一个点Y坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        val secondCFirstPYRatio = 22f / proportion
        mRoundPath.reset()
        mRoundPath.moveTo(width / ratioOffset + left, top) //顶部直线和右上角圆角

        // 顶部直线和右上角圆角
        mRoundPath.lineTo(coerceAtLeast(width / ratioOffset, width - leftRadius * radiusOffsetRatio + left), top)
        mRoundPath.cubicTo(left + width - leftRadius * endPointRatio, top, left + width - leftRadius * firstCSecondPXRatio, top + leftRadius * firstCSecondPYRatio, left + width - leftRadius * firstCThirdPXRatio, top + leftRadius * firstCThirdPYRatio)
        mRoundPath.cubicTo(left + width - leftRadius * secondCFirstPXRatio, top + leftRadius * secondCFirstPYRatio, left + width - leftRadius * secondCFirstPYRatio, top + leftRadius * secondCFirstPXRatio, left + width - leftRadius * firstCThirdPYRatio, top + leftRadius * firstCThirdPXRatio)
        mRoundPath.cubicTo(left + width - leftRadius * firstCSecondPYRatio, top + leftRadius * firstCSecondPXRatio, left + width, top + leftRadius * endPointRatio, left + width, top + coerceAtMost(height / ratioOffset, leftRadius * radiusOffsetRatio))

        mRoundPath.lineTo(left + width, coerceAtLeast(height / ratioOffset, height - topRadius * radiusOffsetRatio) + top)
        mRoundPath.cubicTo(left + width, top + height - topRadius * endPointRatio, left + width - topRadius * firstCSecondPYRatio, top + height - topRadius * firstCSecondPXRatio, left + width - topRadius * firstCThirdPYRatio, top + height - topRadius * firstCThirdPXRatio)
        mRoundPath.cubicTo(left + width - topRadius * secondCFirstPYRatio, top + height - topRadius * secondCFirstPXRatio, left + width - topRadius * secondCFirstPXRatio, top + height - topRadius * secondCFirstPYRatio, left + width - topRadius * firstCThirdPXRatio, top + height - topRadius * firstCThirdPYRatio)
        mRoundPath.cubicTo(left + width - topRadius * firstCSecondPXRatio, top + height - topRadius * firstCSecondPYRatio, left + width - topRadius * endPointRatio, top + height, left + coerceAtLeast(width / ratioOffset, width - topRadius * radiusOffsetRatio), top + height)

        // 底部直线和左下角圆角
        mRoundPath.lineTo(coerceAtMost(width / ratioOffset, rightRadius * radiusOffsetRatio) + left, top + height)
        mRoundPath.cubicTo(left + rightRadius * endPointRatio, top + height, left + rightRadius * firstCSecondPXRatio, top + height - rightRadius * firstCSecondPYRatio, left + rightRadius * firstCThirdPXRatio, top + height - rightRadius * firstCThirdPYRatio)
        mRoundPath.cubicTo(left + rightRadius * secondCFirstPXRatio, top + height - rightRadius * secondCFirstPYRatio, left + rightRadius * secondCFirstPYRatio, top + height - rightRadius * secondCFirstPXRatio, left + rightRadius * firstCThirdPYRatio, top + height - rightRadius * firstCThirdPXRatio)
        mRoundPath.cubicTo(left + rightRadius * firstCSecondPYRatio, top + height - rightRadius * firstCSecondPXRatio, left, top + height - rightRadius * endPointRatio, left, top + coerceAtLeast(height / ratioOffset, height - rightRadius * radiusOffsetRatio))

        mRoundPath.lineTo(left, coerceAtMost(height / ratioOffset, bottomRadius * radiusOffsetRatio) + top)
        mRoundPath.cubicTo(left, top + bottomRadius * endPointRatio, left + bottomRadius * firstCSecondPYRatio, top + bottomRadius * firstCSecondPXRatio, left + bottomRadius * firstCThirdPYRatio, top + bottomRadius * firstCThirdPXRatio)
        mRoundPath.cubicTo(left + bottomRadius * secondCFirstPYRatio, top + bottomRadius * secondCFirstPXRatio, left + bottomRadius * secondCFirstPXRatio, top + bottomRadius * secondCFirstPYRatio, left + bottomRadius * firstCThirdPXRatio, top + bottomRadius * firstCThirdPYRatio)
        mRoundPath.cubicTo(left + bottomRadius * firstCSecondPXRatio, top + bottomRadius * firstCSecondPYRatio, left + bottomRadius * endPointRatio, top, left + coerceAtMost(width / ratioOffset, bottomRadius * radiusOffsetRatio), top)
        mRoundPath.close()
        return mRoundPath
    }
}